# -*- coding: utf-8-*-

from openerp import models,api,SUPERUSER_ID,_
from openerp.osv import osv, fields, orm

# 上下文放入某id测试
class extend_user(models.Model):
    _name = "res.users"
    _inherit = ['res.users']

    # 返回company_partner_id(登录用户的所属partner_id)
    def _company_partner_id(self, cr, uid, user_ids, name, args, context=None):
        result = dict.fromkeys(user_ids)
        for user_id in user_ids:
            cr.execute("SELECT * FROM res_users WHERE id=%s", (user_id,))
            res_user = cr.dictfetchone()
            if res_user['share']:
                partner_id = res_user['partner_id']
            else:
                company_id = res_user['company_id']
                cr.execute("SELECT * FROM res_company WHERE id=%s", (company_id,))
                res_company = cr.dictfetchone()
                partner_id = res_company['partner_id']
            result[user_id] = partner_id
        return result

    # 全局context中放入company_partner_id(登录用户的所属partner_id)
    _columns = {
        'context_company_partner_id': fields.function(_company_partner_id, type="integer"),
    }